<?php
/*
// @author : lael
// blog : http://hi.baidu.com/lael80
// http://www.gzyd.net
// all right reserved.
*/
namespace ydsdk\core;
class common {
	public $config = array();
	public $db = null;
	
	public function __construct(){
		if(!$GLOBALS['YD_SDK']['libraries']['mysqli']){
			include_once YD_ROOT.'config/config.php';
			$this->config = $config;
			$this->db = $this->library('mysqli', $config['db']);
			$error = $this->db->error();
			if($error)exitjson(-1, $error);
			$GLOBALS['YD_SDK']['config'] = $this->config;
		}else{
			$this->config = $GLOBALS['YD_SDK']['config'];
			$this->db = $GLOBALS['YD_SDK']['libraries']['mysqli'];
		}
	}
	
	protected function model($name = '', $params = array()){
		if(!$GLOBALS['YD_SDK']['models'][$name]){
			include_once(YD_ROOT.'models/'.$name.'.php');
			$class = '\ydsdk\models\\'.$name;
			$GLOBALS['YD_SDK']['models'][$name] = new $class($params);
		}
		return $GLOBALS['YD_SDK']['models'][$name];
	}
	
	protected function library($name = '', $params = array()){
		if(!$GLOBALS['YD_SDK']['libraries'][$name]){
			include_once(YD_ROOT.'libraries/'.$name.'.php');
			$class = '\ydsdk\libraries\\'.$name;
			$GLOBALS['YD_SDK']['libraries'][$name] = new $class($params);
		}
		return $GLOBALS['YD_SDK']['libraries'][$name];
	}
	
	//自动处理数据字段
	//$table表
	//$data POST数据
	//$filter要过滤的字段数组比如前端不允许修改账号/状态 $filter = array('account', 'status')
	//$prefix表单name添加了前缀，可为数组，按顺序优先//适用于表单同时插入两个表并有共享字段时
	protected function formdata($table, $data = '', $filter = '', $prefix = ''){
		if($data === '')$data = $_POST;
		if(!is_array($prefix))$prefix = explode(',', $prefix);
		if(!is_array($filter))$filter = explode(',', $filter);
		$fields = $this->db->list_fields($table);
		$result = array();
		foreach($fields as $k => $v){
			if($v['Extra'] == 'auto_increment')continue;
			if(is_array($filter) && in_array($v['Field'], $filter))continue;//排除
			if($v['Field'] == 'password'){
				foreach($prefix as $kk => $vv){
				 	if(isset($data[$vv.$v['Field']])){
						 if(empty($data[$vv.$v['Field']]))continue 2;//空值不修改密码
					}
				}
			}
			$isset = false;
			foreach($prefix as $kk => $vv){
				if(isset($data[$vv.$v['Field']])){
					$result[$v['Field']] = $data[$vv.$v['Field']];
					$isset = true;
					break;
				}
			}
			if($isset){
				$result[$v['Field']] = checknull($result[$v['Field']], checknull($v['Default']));
				//数组
				if(!is_string($result[$v['Field']]))$result[$v['Field']] = json_encode($result[$v['Field']]);
			}
		}
		return $result;
	}

	protected function uploads($params = array()){
		if(empty($_FILES))return false;
		//配置上传参数
		$filepath = $params['filepath']&&$params['upload_path']?$params['filepath']:'data/upload/'.date('Ymd').'/';
		$config = array();
		$config['upload_path']      = $params['filepath']&&$params['upload_path']?$params['upload_path']:YD_PATH.$filepath;
		@mkdir($config['upload_path'], 0777, true);
		$config['allowed_types']    = $params['allowed_types']?:'*';
		$config['max_size']     	= $params['max_size']?:10240;
		$config['max_width']        = $params['max_width']?:0;
		$config['max_height']       = $params['max_height']?:0;
		//载入所需文件上传类库
		$upload = $this->library('upload', $config);
	
		//循环处理上传文件
		$result = array();
		$files = $_FILES;
		foreach ($files as $key => $value) {
			if(is_array($value['name'])){
				$count = count($value['name']);
				for($i = 0; $i < $count; $i ++){
					$_FILES[$key.'#'.$i] = array(
						'name' => time().strrchr($value['name'][$i], '.'),
						'type' => $value['type'][$i],
						'tmp_name' => $value['tmp_name'][$i],
						'error' => $value['error'][$i],
						'size' => $value['size'][$i],
					);
					$result[$key] = array();
					if (!empty($_FILES[$key.'#'.$i]['name'])) {
						if ($upload->do_upload($key.'#'.$i)) {
							//上传成功
							$data = $upload->data();
							$result[$key][$i] = array('filename' => $filepath.$data['file_name'], 'error' => $upload->display_errors()); 
						} else {
							//上传失败
							$result[$key][$i] = array('filename' => '', 'error' => $upload->display_errors()); 
						}
					}else{
						$result[$key][$i] = array('filename' => '', 'error' => '没有选择上传文件'); 
					}			
				}
			}else{
				if (!empty($value['name'])) {
					$value['name'] = time().strrchr($value['name'], '.');
					$_FILES[$key] = $value;
					if ($upload->do_upload($key)) {
						//上传成功
						$data = $upload->data();
						$result[$key] = array('filename' => $filepath.$data['file_name'], 'error' => $upload->display_errors()); 
					} else {
						//上传失败
						$result[$key] = array('filename' => '', 'error' => $upload->display_errors()); 
					}
				}else{
					$result[$key] = array('filename' => '', 'error' => '没有选择上传文件'); 
				}			
			}
		}
		return $result;
	}
	
	public function ueditor(){
		$upload_path = YD_URL.'data/upload/ueditor_'.intval($_SESSION['admin']['admin_id']).'/';
		$upload_allow = $_SESSION['admin']['admin_id'];
		require(YD_ROOT.'libraries/ueditor1_4_3_3-utf8-php/php/controller.php');						   
	}
}
?>